package Android.Billarm;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MoneyScreen extends Activity
{
    private static final int NDIGITS = 7;
    private static final int CUTOFF = 10000000;
    private Bill bill;

	/** Called when the activity is first created. */
	@Override  
	public void onCreate(Bundle state)
    {
		super.onCreate (state);

		setContentView(R.layout.money);

        /* A bill was passed in */
        bill = (Bill) getIntent().getSerializableExtra("bill");

        resetDigits ();
	}

    private void resetDigits ()
    {
        String str = "";
        long x = bill.getAmount ();

        for (int i = 0; i < NDIGITS; ++i)
        {
            if (i == 2)  str = "." + str;
            str = (x % 10) + str;
            x /= 10;
        }
        str = "$" + str;

        ((TextView) findViewById (R.id.moneyScreenDigitText)) .setText (str);
    }

    public void numpadClicked (View v)
    {
        long amount = bill.getAmount ();
        switch (v.getId ())
        {
            case R.id.moneyScreenDigitButton0: amount = 10*amount + 0; break;
            case R.id.moneyScreenDigitButton1: amount = 10*amount + 1; break;
            case R.id.moneyScreenDigitButton2: amount = 10*amount + 2; break;
            case R.id.moneyScreenDigitButton3: amount = 10*amount + 3; break;
            case R.id.moneyScreenDigitButton4: amount = 10*amount + 4; break;
            case R.id.moneyScreenDigitButton5: amount = 10*amount + 5; break;
            case R.id.moneyScreenDigitButton6: amount = 10*amount + 6; break;
            case R.id.moneyScreenDigitButton7: amount = 10*amount + 7; break;
            case R.id.moneyScreenDigitButton8: amount = 10*amount + 8; break;
            case R.id.moneyScreenDigitButton9: amount = 10*amount + 9; break;
            case R.id.moneyScreenDigitButtonD: amount /= 10; break;
            case R.id.moneyScreenDigitButtonC: amount = 0; break;
        }

        if (amount >= CUTOFF)  return;

        bill.setAmount (amount);
        resetDigits ();
    }

    /*-- Exit this screen --*/
    public void doneClicked (View v)
    {
        Intent ret = new Intent ();

        if (v.getId() == R.id.commit)
        {
            ret.putExtra ("bill", bill);
            setResult (RESULT_OK, ret);
        }
        else  setResult (RESULT_CANCELED, ret);

        finish ();
    }
}

